package com.atguigu.yygh.controller;

import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.service.HospitalSetService;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zr
 * @date 2021/12/7 21:13
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Resource
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("removeHospById/{hospId}")
    public Result removeHospById(
            @ApiParam(value = "医院设置ID", name = "hospId", required = true)
            @PathVariable long hospId) {
        boolean flag = hospitalSetService.removeById(hospId);
        return flag ? Result.ok().message("删除成功") : Result.fail().message("删除失败");
    }

    @ApiOperation(value = "条件查询带分页")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        hospitalSetService.findPageHospSet(page, hospitalSetQueryVo);
        return Result.ok(page);
    }

    /**
     * 添加医院设置
     */
    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.saveHospitalSet(hospitalSet);
        return flag ? Result.ok().message("添加成功") : Result.fail().message("添加失败");
    }

    /**
     * 根据Id获取医院设置
     */
    @ApiOperation(value = "根据Id获取医院设置")
    @GetMapping("getHospSet/{hospId}")
    public Result getHospSet(@PathVariable Long hospId) {
        HospitalSet hospitalSet = hospitalSetService.getById(hospId);
        return Result.ok(hospitalSet);
    }

    /**
     * 修改医院设置
     */
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return flag ? Result.ok().message("修改成功") : Result.fail().message("修改失败");
    }

    /**
     * 批量删除医院设置
     */
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> listIds) {
        boolean flag = hospitalSetService.removeByIds(listIds);
        return flag ? Result.ok().message("批量删除成功") : Result.fail().message("批量删除失败");
    }

    /**
     * 医院设置锁定和解锁
     */
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{hospId}/{status}")
    public Result lockHospitalSet(@PathVariable long hospId, @PathVariable int status) {
        // 先通过Id得到医院设置
        HospitalSet hospitalSet = hospitalSetService.getById(hospId);
        // 设置锁定和解锁
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return flag ? Result.ok().message("设置成功") : Result.fail().message("设置失败");
    }

    /**
     * 发送签名key
     */
    @PutMapping("sendKey/{hospId}")
    public Result sendKey(@PathVariable long hospId) {
        HospitalSet hospitalSet = hospitalSetService.getById(hospId);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // 发送短信 TOOD
        return Result.ok();
    }
}
